package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.GetViewPropertiesReq;
import net.ib.mntalk.protocol.Session.GetViewPropertiesRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class GetViewPropertiesTask extends AsyncNetTask {

	private GetViewPropertiesReq mGetViewPropertiesReq;
	
	public GetViewPropertiesTask(Context context, Handler handler, GetViewPropertiesReq getViewPropertiesReq){
		super(context, handler);
		this.mGetViewPropertiesReq = getViewPropertiesReq;
	}
	
	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mGetViewPropertiesReq)
			.setMethodId(Session.View.GetViewProperties)
			.build();
		return rev;
	}
	
	@Override
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("GetViewPropertiesTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetViewPropertiesRes getViewPropertiesRes = (GetViewPropertiesRes) resultEnvelope.getMessage();
		
		if(getViewPropertiesRes.getResponseCode() == Common.SUCCESS){
			// TODO : 디비작업
//			InboxDatabaseHelper.updateView(mQueryHandler, viewInfo);
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_VIEW_PROPERTIES_SUCCESS));
		} else{
			// TODO : 디비작업
//			InboxDatabaseHelper.updateView(mQueryHandler, viewInfo);
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_VIEW_PROPERTIES_FAIL));
		}
	}
}
